<template>
  <div class="search">
    <!-- 1. 输入搜索关键字的表单 -->
    <el-form :model="searchForm" label-width="80px" size="large" ref="formRef">
      <el-row>
        <el-col :span="8">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="searchForm.name" placeholder="请输入查询的角色名称" />
          </el-form-item>
        </el-col>
        <!-- 2.重置和搜索的按钮 -->
        <el-col :span="2" :offset="1">
          <el-button icon="Search" type="primary" @click="handleQueryClick">搜索</el-button>
        </el-col>
        <el-col :span="2">
          <el-button icon="Refresh" @click="handleResetClick">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ElForm } from 'element-plus'
import { reactive, ref } from 'vue'

// 定义自定义事件
const emit = defineEmits(['queryClick', 'resetClick'])

const searchForm = reactive({
  name: '',
  realname: '',
  cellphone: '',
  enable: 1,
  createAt: ''
})

// 重置操作
const formRef = ref<InstanceType<typeof ElForm>>()
function handleResetClick() {
  // 1. form中的数据全部重置
  formRef.value?.resetFields()
  // 2. 将事件出去。content内部重新发送网络请求
  emit('resetClick')
}

function handleQueryClick() {
  emit('queryClick', searchForm)
}
</script>

<style lang="less" scoped>
.search {
  background-color: #fff;
  padding: 20px;
}

</style>
